package com.ggh.jinjilive.ui.home.follow;

import android.os.Bundle;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;

import androidx.annotation.NonNull;
import androidx.annotation.Nullable;
import androidx.lifecycle.Observer;
import androidx.lifecycle.ViewModelProvider;
import androidx.recyclerview.widget.GridLayoutManager;

import com.blankj.utilcode.util.ConvertUtils;
import com.ggh.jinjilive.BaseApplication;
import com.ggh.jinjilive.R;
import com.ggh.jinjilive.databinding.FragmentFollowBinding;
import com.ggh.jinjilive.mobile.home.live.LiveList;
import com.ggh.jinjilive.pagestatus.EmptyCallback;
import com.ggh.jinjilive.pagestatus.LoadingCallback;
import com.ggh.jinjilive.ui.BaseFragment;
import com.ggh.jinjilive.ui.home.follow.adapter.FollowAdapter;
import com.ggh.jinjilive.views.GridItemDecoration;
import com.kingja.loadsir.callback.Callback;
import com.kingja.loadsir.core.LoadSir;
import com.scwang.smart.refresh.footer.ClassicsFooter;
import com.scwang.smart.refresh.header.MaterialHeader;
import com.scwang.smart.refresh.layout.api.RefreshLayout;
import com.scwang.smart.refresh.layout.listener.OnLoadMoreListener;
import com.scwang.smart.refresh.layout.listener.OnRefreshListener;

import org.jetbrains.annotations.NotNull;

import java.util.List;

public class FollowFragment extends BaseFragment<FragmentFollowBinding> implements OnRefreshListener, OnLoadMoreListener {
    private FollowViewModel followViewModel;
    //关注适配器
    private FollowAdapter followAdapter;

    @Override
    public void onCreate(@Nullable @org.jetbrains.annotations.Nullable Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        BaseApplication.getInstance().getAppComponent().doInjectionMain(this);
        followViewModel = new ViewModelProvider(this,viewModelFactory).get(FollowViewModel.class);
    }

    @Override
    public int initContentView(LayoutInflater inflater, @Nullable @org.jetbrains.annotations.Nullable ViewGroup container, @Nullable @org.jetbrains.annotations.Nullable Bundle savedInstanceState) {
        return R.layout.fragment_follow;
    }

    @Override
    public void onViewCreated(@NonNull View view, @Nullable Bundle savedInstanceState) {
        super.onViewCreated(view, savedInstanceState);
        View contentView = binding.getRoot().findViewById(R.id.refreshLayout);
        loadService = LoadSir.getDefault().register(contentView, new Callback.OnReloadListener() {
            @Override
            public void onReload(View v) {
                loadService.showCallback(LoadingCallback.class);
                onRefresh(binding.refreshLayout);
            }
        });
        loadService.showCallback(LoadingCallback.class);
        onRefresh(binding.refreshLayout);
    }


    @Override
    public void initView() {
        super.initView();

        binding.refreshLayout.setRefreshHeader(new MaterialHeader(mContext));
        binding.refreshLayout.setRefreshFooter(new ClassicsFooter(mContext));

    }

    @Override
    public void initData() {
        super.initData();
        followAdapter = new FollowAdapter();
        binding.recyclerView.setLayoutManager(new GridLayoutManager(mContext,2));
        binding.recyclerView.addItemDecoration(new GridItemDecoration(2, ConvertUtils.dp2px(10),true));
        binding.recyclerView.setAdapter(followAdapter);

        //关注
        followViewModel.mutableLiveData.observe(this, new Observer<List<LiveList>>() {
            @Override
            public void onChanged(List<LiveList> liveLists) {
                binding.refreshLayout.finishRefresh();//结束刷新
                if(followViewModel.isRefresh){
                    if(null != liveLists && liveLists.size() > 0){
                        followAdapter.setAdd(liveLists);
                        loadService.showSuccess();
                    }else {
                        loadService.showCallback(EmptyCallback.class);
                    }
                }else {
                    if(null != liveLists && liveLists.size() > 0){
                        followAdapter.Add(liveLists);
                        binding.refreshLayout.finishLoadMore();//结束刷新
                    }else {
                        binding.refreshLayout.finishLoadMoreWithNoMoreData();//结束刷新
                    }
                }
            }
        });
    }

    @Override
    public void initViewObservable() {
        super.initViewObservable();
        //刷新
        binding.refreshLayout.setOnRefreshListener(this);
        //加载
        binding.refreshLayout.setOnLoadMoreListener(this);
    }

    @Override
    public void onRefresh(@NonNull @NotNull RefreshLayout refreshLayout) {
        followViewModel.isRefresh = true;
        followViewModel.followList();
    }

    @Override
    public void onLoadMore(@NonNull @NotNull RefreshLayout refreshLayout) {
        followViewModel.isRefresh = false;
        followViewModel.followList();
    }


}
